﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TomNet.Redis;

namespace CommServer.Forms
{
    public partial class FrmNodes : Form
    {
        private RedisClient redisClient;
        private static readonly string NodesKey = "NodeServers";

        private List<string> nodes;
        public FrmNodes()
        {
            InitializeComponent();
            redisClient = new RedisClient();
        }

        private void FrmNodes_Load(object sender, EventArgs e)
        {
            InitData();
        }


        private void InitData()
        {
            try
            {
                nodes = redisClient.ListRange<string>(NodesKey);
                if (nodes != null)
                {
                    lvNodes.BeginUpdate();
                    lvNodes.Items.Clear();
                    for (int i = 0; i < nodes.Count; i++)
                    {
                        ListViewItem lvi = new ListViewItem();
                        lvi.Text = (i + 1).ToString();
                        lvi.SubItems.Add((i + 1).ToString());
                        lvi.SubItems.Add(nodes[i]);
                        lvNodes.Items.Add(lvi);
                    }
                    lvNodes.EndUpdate();
                }
            }
            catch { }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var ip = txtIp.Text.Trim();
            if (string.IsNullOrEmpty(ip))
            {
                ShowMessage("IP地址不能为空！");
                txtIp.Focus();
                return;
            }

            IPAddress ipa;
            if (!IPAddress.TryParse(ip, out ipa))
            {
                ShowMessage("IP地址不正确！");
                txtIp.Focus();
                return;
            }

            if (nodes.Exists(m => m.Equals(ip)))
            {
                ShowMessage("IP重复！");
                txtIp.Focus();
                return;
            }
            try
            {
                redisClient.ListRightPush(NodesKey, ip);
                txtIp.Focus();
            }
            catch
            {
                ShowMessage("添加结点服务器出错！");
            }
            InitData();
        }

        private void ShowMessage(string msg, string caption = "温馨提示")
        {
            MessageBox.Show(msg, caption);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lvNodes.SelectedItems.Count > 0)
            {
                string ip = lvNodes.SelectedItems[0].SubItems[2].Text;

                try
                {
                    redisClient.ListRemove<string>(NodesKey, ip);
                    txtIp.Focus();
                }
                catch
                {
                    ShowMessage("删除结点服务器出错！");
                }
                InitData();
            }
        }
    }
}
